<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use Hash;

class LoginController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function login()
    {
        return view('admin.login');
    }

    //登录验证
    public function dologin(Request $request)
    {	
    	$username = $request ->username;
    	//读取
    	$user = User::where('username',$username)->first();
    	//判断
    	if(empty($user)) {
    		return back()->with('info','用户不存在');
    	}

    	//判断密码
    	if(Hash::check($request->password,$user->password)){
            session(['id'=>$user->id]);

    		return redirect('/admin')->with('info','登录成功');
    	}else{
    		return back()->with('info','密码错误');
    	}
        //生成session
        // $request->session()->put('username', '$user');

    }
    public function logout(Request $request)
    {
        $request->session()->flush();
        return redirect('/admin/login');
    }
}
